<?php

namespace Home\Controller;
use Think\Controller;

/**
* 附近站点控制器
* @autho cheng
*/
class NearbyStationController extends BaseController {

    /**
     * 查询附近站点接口
     * @again quyang 2017-1-18
     * @autho cheng
     */
    public function queryNearbyStation() {
        $param = I('param.');
        if (empty($param['areaId']) || empty($param['expId']) || $param['branchState'] == '') {
            $this->param_error();
        }
        $map['u.f_area_id'] = $param['areaId']; //区/县Id
        $map['u.f_user_type_id'] = 0; //用户类型 0=站点
        //获取未合作站点
        if ($param['branchState'] == 2) {
            $map['_string'] = 'NOT EXISTS (select d.f_station_id from tp_express_branch_com_dz d where d.f_exp_id =  '.$param['expId'].' and d.f_station_id = u.f_user_id)';
            $map['u.f_user_status'] = 1 ;   // 用户状态：1 启用
            $map['u.f_addition_status'] = 1 ;   // 审核状态：1 审核通过
        }
        //获取已合作站点
        else
        {
            //查询 快递员 允许 入库的 站点列表
            $continual_useSql = isset( $param['continual_use'] ) ? " AND d.continual_use = 0" : '';

            $map['_string'] = 'EXISTS (select d.f_station_id from tp_express_branch_com_dz d where d.f_exp_id =  '.$param['expId'].' and d.f_branch_state= '.$param['branchState']. $continual_useSql .' and d.f_station_id = u.f_user_id)';
        }
        
        $page = !empty($param['currentPage']) ? $param['currentPage'] : 1; //当前页
        $nums = !empty($param['pageSize']) ? $param['pageSize'] : 10; //页大小
        $data = D('Home/User')->getNearbyStation($map, $page, $nums, $param['branchState'], $param['expId']);

        $this->output($status = 1, $data);
    }
    
    /**
     * @desc:  快递员申请合作接口
     * @Author: zhoutai
     * @update: 16/11/24
     **/

    public function applyCo(){

		$data = I('param.');
		if (empty($data['fExpId']) || empty($data['fComId']) || empty($data['fExpName']) || empty($data['fComName']) || empty($data['fStationId'])){
			$this->param_error();
		}

		$tpEBCDT = D('tp_express_branch_com_dz');
		$condition['f_com_id'] = $data['fComId'];
		$condition['f_station_id'] = $data['fStationId'];
		$condition['f_exp_id'] = $data['fExpId'];

		$exit = $tpEBCDT->where($condition)->field('f_exp_id')->find();
		if ($exit) $this->auto_error('已申请过该站点合作');

		$where['f_com_id'] = $data['fComId'];
		$where['f_station_id'] = $data['fStationId'];
		$where['f_branch_state'] = '0';

        //已有相同快递公司合作
		$return = $tpEBCDT->where($where)->field('f_exp_id')->find();
		if ($return) $this->output(false);

		$insertData = array(
			'f_exp_id' => $data['fExpId'],
			'f_branch_id' => 1,
			'f_com_id'  => $data['fComId'],
			'f_exp_name'   => $data['fExpName'],
			'f_branch_name' => '',
			'f_com_name' => $data['fComName'],
			'f_station_id' => $data['fStationId'],
			'f_branch_state' => '1',
            'f_create_time'=>time()
		);
		$insert = $tpEBCDT->add($insertData);
		if ($insert){
			$this->output(1);
		}
    }



}
